<?php

namespace app\manystore\controller;

use app\common\controller\ManystoreBase;
use app\common\model\Advance;
use app\common\model\Pauserecords;
use app\common\model\Reservations;

/**
 * 桌游参与用户管理
 *
 * @icon fa fa-circle-o
 */
class Occupants extends ManystoreBase
{

    /**
     * Occupants模型对象
     * @var \app\manystore\model\Occupants
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\manystore\model\Occupants;

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user','rooms','manystoreshop','lights'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {

                if($row->status==2){
                    $info= Advance::where($row->id)->find();
                    $row->playmoney=$info['play'];
                    $row->goodmoney=$info['goods'];;
                }
                if($row->status==1){
                   $hour=ceil(($row['leave_time']-$row['join_time'])/3600);
                    $row->playmoney=$hour*$row['unit_price'];
                    $row->goodmoney=\app\manystore\model\Goodorder::where('occupants_id',$row->id)->sum('total');
                }
                if($row->status==0){
                    $hour=ceil((time()-$row['join_time'])/3600);
                    $row->playmoney=$hour*$row['unit_price'];
                    $row->goodmoney=\app\manystore\model\Goodorder::where('occupants_id',$row->id)->sum('total');

                    //如果是预约单
                    if($row['reservations_id']!=null){
                        $Reservations=Reservations::where('id','=',$row['reservations_id'])->find();
                        $row->leave_time=$Reservations['end_time'];
                    }
                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function end_order(){
        if (!$id = input('id/d')) {
            return json(['code'=>0,'msg'=>'订单ID不能为空']);
        }
        $occupants= \app\common\model\Occupants::where('id',$id)->find();
        if($occupants['status']!=0){
            return json(['code'=>0,'msg'=>'当前订单已结束']);
        }
        \app\manystore\model\Occupants::where('id',$id)->update(['status'=>1,'leave_time'=>time()]);
        return json(['code'=>1,'msg'=>'成功']);
    }


    public function pause_order()
    {
        if (!$id = input('id/d')) {
            return json(['code' => 0, 'msg' => '订单ID不能为空']);
        }
        $Occupants = \app\common\model\Occupants::where('id', $id)->find();
        $Pauserecords = Pauserecords::where('occupant_id', $id)->where('status', 1)->find();
        if ($Pauserecords) {
            $time = time();
            Pauserecords::where('id', '=', $Pauserecords['id'])->update(['resume_time' => $time, 'status' => 2]);
            \app\common\model\Occupants::where('id', $id)->update(['pause_time' => $Occupants['pause_time'] + ($time - $Pauserecords['pause_time']), 'pause_status' => 1]);
            return json(['code' => 1, 'msg' => '已恢复']);
        } else {
            Pauserecords::create(['user_id' => $Occupants['user_id'], 'occupant_id' => $id, 'pause_time' => time(), 'status' => 1]);
            \app\common\model\Occupants::where('id', $id)->update(['pause_status' => 2]);
            return json(['code' => 1, 'msg' => '已暂停']);
        }

    }

    public function send_gift()
    {
        if (!$id = input('id/d')) {
            return json(['code' => 0, 'msg' => '订单ID不能为空']);
        }
        $Occupants = \app\common\model\Occupants::where('id', $id)->find();
        if($Occupants['status']!=0){
            return json(['code' => 0, 'msg' => '订单已结束，无法赠送']);
        }
        \app\manystore\model\Occupants::where('id',$id)->update(['gift_time'=>$Occupants['gift_time']+input('minutes')*60]);
        return json(['code' => 1, 'msg' => '赠送成功']);

    }

    public function delay_action()
    {
        if (!$id = input('id/d')) {
            return json(['code' => 0, 'msg' => '订单ID不能为空']);
        }
        $Occupants = \app\common\model\Occupants::where('id', $id)->find();
        if ($Occupants['status'] != 0) {
            return json(['code' => 0, 'msg' => '订单已结束']);
        }
        if ($Occupants['reservations_id'] == null) {
            return json(['code' => 0, 'msg' => '此订单非预约单']);
        }
        $Reservations = Reservations::where('id', '=', $Occupants['reservations_id'])->find();
        $datetime = input('datetime');
        if($datetime==''){
            return json(['code' => 0, 'msg' => '请选择时间']);
        }
        if ($datetime < $Reservations['end_time']) {
            return json(['code' => 0, 'msg' => '选择的时间小于原时间']);
        }
       Reservations::where('id', '=', $Occupants['reservations_id'])->update(['end_time'=>$datetime]);
        return json(['code' => 1, 'msg' => '延时成功']);
    }
}
